package com.guyu.plugin.sms.suoyou.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.guyu.plugin.sms.SmsSupplierConstant;
import com.guyu.plugin.sms.suoyou.config.SuoyouConfig;
import com.guyu.plugin.sms.suoyou.utils.SuoyouUtils;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.constant.Constant;
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.comm.utils.SmsUtils;
import org.dromara.sms4j.provider.service.AbstractSmsBlend;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;


@Slf4j
public class SuoyouSmsImpl extends AbstractSmsBlend<SuoyouConfig> {

    private int retry = 0;

    public SuoyouSmsImpl(SuoyouConfig config, Executor pool, DelayedTime delayed) {
        super(config, pool, delayed);
    }

    public SuoyouSmsImpl(SuoyouConfig config) {
        super(config);
    }

    @Override
    public String getSupplier() {
        return SmsSupplierConstant.SUOYOU;
    }

    @Override
    public SmsResponse sendMessage(String phone, String message) {
        Map<String, Object> paramMap = SuoyouUtils.generateParamMap(getConfig(), phone, message);
        String url = Constant.HTTPS_PREFIX + getConfig().getRequestUrl() + getConfig().getAction();
        try {
            SmsResponse smsResponse = getResponse(HttpUtil.post(url, paramMap, 10000));
            if (smsResponse.isSuccess() || retry == getConfig().getMaxRetries()) {
                retry = 0;
                return smsResponse;
            }
            return requestRetry(phone, message);
        } catch (SmsBlendException e) {
            return requestRetry(phone, message);
        }
    }

    @Override
    public SmsResponse sendMessage(String phone, LinkedHashMap<String, String> messages) {
        return sendMessage(phone, SuoyouUtils.generateMessage(getConfig(), messages));
    }


    private SmsResponse requestRetry(String phone, String message) {
        http.safeSleep(getConfig().getRetryInterval());
        retry++;
        log.warn("短信第 {} 次重新发送", retry);
        return sendMessage(phone, message);
    }

    @Override
    public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
        return sendMessage(phone, SuoyouUtils.generateMessage(getConfig(), messages));
    }

    @Override
    public SmsResponse massTexting(List<String> phones, String message) {
        if (phones.size() > 500) {
            throw new SmsBlendException("单次发送超过最大发送上限，建议每次群发短信人数低于500");
        }
        return sendMessage(SmsUtils.joinComma(phones), message);
    }

    @Override
    public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
        if (phones.size() > 500) {
            throw new SmsBlendException("单次发送超过最大发送上限，建议每次群发短信人数低于500");
        }
        return sendMessage(SmsUtils.joinComma(phones), SuoyouUtils.generateMessage(getConfig(), messages));
    }


    private SmsResponse getResponse(String result) {
        List<String> list = StrUtil.split(result, "|");
        SmsResponse smsResponse = new SmsResponse();
        smsResponse.setSuccess(Objects.equals(list.get(0), "1000"));
        smsResponse.setData(list.get(1));
        smsResponse.setConfigId(getConfigId());
        return smsResponse;
    }
}
